
//
//  DiscoverMCircleListViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/14.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "PlatformProductTypeListViewController.h"
#import "RDVTabBarController.h"
#import "PlatformProductTypeListCell.h"
#import "MCirclemodel.h"
#import "UIImageView+WebCache.h"

#import "HttpRequest.h"
#import "TheLoginViewController.h"

@interface PlatformProductTypeListViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,PlatformProductTypeListCellDelegate>
@property (strong, nonatomic)UITableView *tableView;
@property (nonatomic, strong) UIImageView * allImageBtn;
@property(nonatomic,strong)NSString*sta;
@end

@implementation PlatformProductTypeListViewController{
    int index;
    NSString * hasNext;
    NSString *nextIndex;
    NSMutableArray *allComment;
    NSMutableArray *amt;
    NSMutableArray *detail;
    NSMutableArray *provinceName;
    NSMutableArray *cityName;
    NSMutableArray *status;
    NSMutableArray*imageUrlFull;
    NSMutableArray *time;
    NSMutableArray *reqId;
    NSMutableArray *address;
    NSMutableArray *userName;
    UIButton*sat;
    UIButton*sat1;
    UIButton*sxuan;
    int tapImageCount;
    
    NSMutableArray*arr;
}
@synthesize mDelegate;
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    [self.tableView removeFromSuperview];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor=zuiqianhuiColor;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"PlatformProductTypeListCell" bundle:nil] forCellReuseIdentifier:@"PlatformProductTypeListCell"];
    [self.view addSubview:self.tableView];
    [self setup];
    
    
    UIButton*sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60, 60/2)];
    [sxBtn setTitle:@"确认" forState:UIControlStateNormal];
    sxBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [sxBtn setTitleColor:luseColor forState:UIControlStateNormal];
    [sxBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
}

-(void)addProduct:(UIButton*)sender{
    [mDelegate checkVouchType:self.content checkVouchTypeCode:self.code];
     [self.navigationController popViewControllerAnimated:YES];



}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择类别";
   
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    
    
    
}
- (void)setup{
    
    amt=[[NSMutableArray alloc]init];
    detail=[[NSMutableArray alloc]init];
    provinceName=[[NSMutableArray alloc]init];
    status=[[NSMutableArray alloc]init];
    time=[[NSMutableArray alloc]init];
    reqId=[[NSMutableArray alloc]init];
    address=[[NSMutableArray alloc]init];
    userName=[[NSMutableArray alloc]init];
    cityName=[[NSMutableArray alloc]init];
    imageUrlFull=[[NSMutableArray alloc]init];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
//    [params setValue:@"100" forKey:@"pageSize"];
//    [params setValue:@"1" forKey:@"pageIndex"];
    NSLog(@"-----%@",params);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    [HttpRequest requestData:@"Platform/ProductType/List" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"1133333-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                arr=[severDataDict.dataDict objectForKey:@"DataLine"];
                [self setupCouponCommentView:severDataDict.dataDict];
                for (NSDictionary* dicc in arr )
                {
                    
                    [reqId addObject:[dicc valueForKey:@"parentID"]];
                }
                }else{
                
                
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
                
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    TheLoginViewController*detailViewController = [[TheLoginViewController alloc] init];
                    
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
    
    
}

- (void)setupCouponCommentView:(NSDictionary *)dic{
    
    NSMutableArray *commentArray = [dic objectForKey:@"DataLine"];
    allComment = [NSMutableArray array];
    for (int i = 0; i<commentArray.count; i++) {
        
        NSMutableDictionary *dataDic = commentArray[i];
        if (![dataDic isKindOfClass:[NSNull class]]){
            
            NSArray *commentPicArray = [dataDic objectForKey:@"childrens"];
            
            MCirclemodel *cModel = [[MCirclemodel alloc]init];
            cModel.commentId = [dataDic objectForKey:@"typeCode"];
            cModel.userName = [dataDic objectForKey:@"typeName"];

            NSMutableArray *typeCode = [NSMutableArray array];
            NSMutableArray *typeName = [NSMutableArray array];
            for (NSDictionary *picDic in commentPicArray) {
                [typeCode addObject:[picDic objectForKey:@"typeCode"]];
                [typeName addObject:[picDic objectForKey:@"typeName"]];
            }
            cModel.typeCode = typeCode;
            cModel.typeName = typeName;
            [allComment addObject:cModel];
        }
    }
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}



#pragma mark - TableView dataSource && delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      
    return allComment.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MCirclemodel *weibo = allComment[indexPath.row];
    CGFloat collectionHeight=0;
    CGFloat headImageHeight = 60;
    
    for (int i = 0,j = 0; i < weibo.typeCode.count; i++) {
        if (i % 4 == 0) {
            j++;
        }
        collectionHeight = (j+1)*30;
    }

    return  headImageHeight+collectionHeight+20;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"PlatformProductTypeListCell";
    PlatformProductTypeListCell *cell = (PlatformProductTypeListCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"PlatformProductTypeListCell" owner:self options:nil][0];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MCirclemodel *obj = allComment[indexPath.row];
    cell.comment = obj;
    cell.content=self.content;
    cell.mDelegate=self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
}
#pragma mark - CommentCellDelegate
-(void)code:(NSString *)code content:(NSString *)content{
    
    self.code=code;
    self.content=content;
    [self.tableView reloadData];

}


-(NSString *)ret32bitString:(NSInteger)i

{
    
    char data[i];
    
    for (int x=0;x<i;data[x++] = (char)('A' + (arc4random_uniform(26))));
    
    return [[NSString alloc] initWithBytes:data length:i encoding:NSUTF8StringEncoding];
    
}
-(int)getRandomNumber:(int)from to:(int)to

{
    
    return (int)(from + (arc4random() % (to - from + 1)));
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
